package logic

import (
	"an_ju_ke/models"
	"an_ju_ke/myrpc/internal/svc"
	"an_ju_ke/myrpc/myrpc"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type RestLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RestLogic {
	return &RestLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RestLogic) Rest(in *myrpc.RestRequest) (*myrpc.RestResponse, error) {
	// todo: add your logic here and delete this line
	key := fmt.Sprintf("sms_captcha_user_phone:%s", in.UserPhone)
	get, _ := models.Rdb.Get(models.Ctx, key).Result()
	if get != in.Code {
		return &myrpc.RestResponse{}, fmt.Errorf("验证码错误")
	}
	user := models.User{
		UserPhone:    in.UserPhone,
		UserName:     in.UserName,
		UserPassword: in.UserPassword,
	}
	if models.DB.Create(&user).Error != nil {
		return &myrpc.RestResponse{}, fmt.Errorf("数据库繁忙")

	}
	models.Rdb.Del(models.Ctx, key)
	return &myrpc.RestResponse{}, nil
}
